<?php

// +----------------------------------------------------------------------
// | 版权所有 2017-2027 云南环拓科技有限公司，并保留所有权利。
// +----------------------------------------------------------------------
// | Copyright (c) 2017-2027 http://www.yn123.com All rights reserved.
// +----------------------------------------------------------------------
// | Notice: This code is not open source, it is strictly prohibited
// |         to distribute the copy, otherwise it will pursue its
// |         legal responsibility.
// | 注意：  这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用,
// |        不允许对程序代码以任何形式任何目的的再发布。
// +----------------------------------------------------------------------
// | Author: feraner <feraner@163.com> 
// | Date: 2019/12/26
// +----------------------------------------------------------------------

namespace app\common\services;
use app\common\facade\Arr;
use think\Db;

/**
 *  发布二手商品计算类
 * Class MarketPublishComputerService
 * @package app\common\services
 * @author feraner<feraner@163.com>
 * @date 2019/12/26
 */
class MarketPublishComputerService
{


    /**
     * 发布二手商品需要的积分或发布豆
     * @param $goodsInfo
     *
     * @return array
     * @throws \Exception
     */
    public function needWealth($goodsInfo)
    {
        try {
            //1： 获取所属代理商
            $agencyInfo = $this->__getAgencyInfo($goodsInfo);
            if ($agencyInfo) {
                //2: 代理商二手发布配置
                $agencyPublishInfo = Db::name('bean')
                    ->where('aid', Arr::get($agencyInfo, 'id'))
                    ->find() ?: [];

                //3: 二手商城信息 [*代理商未配置读取系统配置]
                //发布一个二手商品需要几个积分
                $publishGoodsCredits = Arr::get($agencyPublishInfo, 'market_credits', (sysconf('publish_market_goods_need_credits') ?: 0));
                //发布一个二手商品需要几个发布豆
                $publishGoodsBean = Arr::get($agencyPublishInfo, 'market_bean',(sysconf('publish_market_goods_need_bean') ?: 0));

                return [
                    'needBean'      => $publishGoodsBean ?: 0,
                    'needCredits'   => $publishGoodsCredits ?: 0,
                ];
            }
            throw new \Exception('未找到代理商');
        } catch (\Exception $e) {
            throw $e;
        }

    }

    /**
     * 依据代理商获取地区ID
     * @param $jobInfo
     *
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    private function __getAgencyInfo($goodsInfo)
    {
        //这里的商品信息 的地区可能有两种格式 1：添加 2：来自数据库
        $provincesId = Arr::get($goodsInfo, 'provincesId', Arr::get($goodsInfo, 'provinces_id', 0));
        $cityId = Arr::get($goodsInfo, 'cityId', Arr::get($goodsInfo, 'city_id', 0));
        $countyId = Arr::get($goodsInfo, 'countyId', Arr::get($goodsInfo, 'county_id', 0));

        $agencyInfo = Db::name('system_agent_user')
            ->where('provinces_id', $provincesId)
            ->where('city_id', $cityId)
            ->where('status', 1)
            ->where('is_deleted', 0)
            ->where('pid', 0)
            ->find() ?: [];

        return $agencyInfo;
    }


}
